#include <QApplication>
#include <QMainWindow>
#include <QVBoxLayout>
#include <QMenuBar>
#include <QStatusBar>
#include <QAction>
#include <QMessageBox>
#include <QDir>
#include <QDebug>
#include "sonyresponsiveexample.h"
#include "responsivetest.h"

/**
 * 🎨 SonyResponsiveDemo - Sony Ive响应式系统演示程序
 *
 * 完整展示Sony Ive响应式文件列表系统的功能
 */

class SonyResponsiveDemo : public QMainWindow
{
    Q_OBJECT

public:
    SonyResponsiveDemo(QWidget* parent = nullptr);
    ~SonyResponsiveDemo() = default;

private slots:
    void showAbout();
    void showHelp();
    void runSystemTest();
    void showPerformanceMonitor();
    void resetToDefaults();

private:
    void setupMenus();
    void setupCentralWidget();
    void setupStatusBar();

    SonyResponsiveExample* m_exampleWidget;
    ResponsiveTest* m_testRunner;
    QLabel* m_statusLabel;
};

SonyResponsiveDemo::SonyResponsiveDemo(QWidget* parent)
    : QMainWindow(parent)
    , m_exampleWidget(nullptr)
    , m_testRunner(nullptr)
    , m_statusLabel(nullptr)
{
    setWindowTitle("🎨 Sony Ive 响应式文件列表系统 - 演示程序");
    setMinimumSize(1400, 900);

    setupMenus();
    setupCentralWidget();
    setupStatusBar();

    // 初始化测试运行器
    m_testRunner = new ResponsiveTest(this);

    statusBar()->showMessage("🎨 Sony Ive 响应式系统已就绪", 3000);

}

void SonyResponsiveDemo::setupMenus()
{
    // 文件菜单
    QMenu* fileMenu = menuBar()->addMenu("文件(&F)");

    QAction* openAction = fileMenu->addAction("打开文件夹(&O)");
    openAction->setShortcut(QKeySequence::Open);
    connect(openAction, &QAction::triggered, this, [this]() {
        QMessageBox::information(this, "提示", "在实际应用中，这里会打开文件夹选择对话框");
    });

    fileMenu->addSeparator();

    QAction* exitAction = fileMenu->addAction("退出(&X)");
    exitAction->setShortcut(QKeySequence::Quit);
    connect(exitAction, &QAction::triggered, this, &QWidget::close);

    // 视图菜单
    QMenu* viewMenu = menuBar()->addMenu("视图(&V)");

    QAction* resetViewAction = viewMenu->addAction("重置视图(&R)");
    connect(resetViewAction, &QAction::triggered, this, &SonyResponsiveDemo::resetToDefaults);

    viewMenu->addSeparator();

    QAction* performanceAction = viewMenu->addAction("性能监控器(&P)");
    connect(performanceAction, &QAction::triggered, this, &SonyResponsiveDemo::showPerformanceMonitor);

    // 工具菜单
    QMenu* toolsMenu = menuBar()->addMenu("工具(&T)");

    QAction* testAction = toolsMenu->addAction("运行系统测试(&T)");
    connect(testAction, &QAction::triggered, this, &SonyResponsiveDemo::runSystemTest);

    // 帮助菜单
    QMenu* helpMenu = menuBar()->addMenu("帮助(&H)");

    QAction* helpAction = helpMenu->addAction("使用帮助(&H)");
    connect(helpAction, &QAction::triggered, this, &SonyResponsiveDemo::showHelp);

    helpMenu->addSeparator();

    QAction* aboutAction = helpMenu->addAction("关于(&A)");
    connect(aboutAction, &QAction::triggered, this, &SonyResponsiveDemo::showAbout);
}

void SonyResponsiveDemo::setupCentralWidget()
{
    m_exampleWidget = new SonyResponsiveExample(this);
    setCentralWidget(m_exampleWidget);
}

void SonyResponsiveDemo::setupStatusBar()
{
    m_statusLabel = new QLabel("就绪", this);
    statusBar()->addWidget(m_statusLabel);

    QLabel* versionLabel = new QLabel("v1.0 - Sony Ive Design", this);
    versionLabel->setStyleSheet("color: #666; font-style: italic;");
    statusBar()->addPermanentWidget(versionLabel);
}

void SonyResponsiveDemo::showAbout()
{
    QString aboutText = QString(
        "<h3>🎨 Sony Ive 响应式文件列表系统</h3>"
        "<p>版本: 1.0</p>"
        "<p><b>核心理念:</b></p>"
        "<ul>"
        "<li>渐进式信息显示 - 根据可用空间智能调整信息密度</li>"
        "<li>优雅视觉过渡 - Sony风格的微妙渐变和阴影效果</li>"
        "<li>流畅动画响应 - 触觉反馈式的交互动画</li>"
        "<li>智能性能优化 - 自适应缓存和节流机制</li>"
        "</ul>"
        "<p><b>技术特点:</b></p>"
        "<ul>"
        "<li>5种响应式显示模式: Micro, Compact, Standard, Expanded, Full</li>"
        "<li>单例工厂模式确保组件一致性</li>"
        "<li>实时性能监控和自动优化</li>"
        "<li>完整的测试覆盖和验证系统</li>"
        "</ul>"
        "<p><b>显示模式阈值:</b></p>"
        "<ul>"
        "<li>Micro: &lt; 180px - 图标 + 极简文件名</li>"
        "<li>Compact: 180-250px - + 状态指示</li>"
        "<li>Standard: 250-350px - + 进度 + 页码</li>"
        "<li>Expanded: 350-450px - + 布局模式</li>"
        "<li>Full: &gt; 450px - 完整信息展示</li>"
        "</ul>"
        "<p><em>采用Sony Ive设计哲学，打造极致用户体验</em></p>"
    );

    QMessageBox::about(this, "关于 Sony Ive 响应式系统", aboutText);
}

void SonyResponsiveDemo::showHelp()
{
    QString helpText = QString(
        "<h3>🎨 使用指南</h3>"
        "<p><b>基本操作:</b></p>"
        "<ul>"
        "<li><b>响应式模式:</b> 通过控制面板启用/禁用响应式布局</li>"
        "<li><b>动画控制:</b> 调整Sony风格动画效果和参数</li>"
        "<li><b>性能优化:</b> 启用自动优化和缓存机制</li>"
        "</ul>"
        "<p><b>测试功能:</b></p>"
        "<ul>"
        "<li><b>性能测试:</b> 运行完整的系统性能测试</li>"
        "<li><b>生成报告:</b> 查看详细的性能和使用报告</li>"
        "<li><b>系统测试:</b> 验证所有组件功能</li>"
        "</ul>"
        "<p><b>调试信息:</b></p>"
        "<ul>"
        "<li><b>性能监控:</b> 实时查看FPS、内存和缓存状态</li>"
        "<li><b>警告日志:</b> 接收性能优化建议</li>"
        "<li><b>状态显示:</b> 了解当前系统运行状态</li>"
        "</ul>"
        "<p><b>开发信息:</b></p>"
        "<ul>"
        "<li>所有调试信息输出到控制台</li>"
        "<li>支持Qt 6.x及以上版本</li>"
        "<li>采用现代C++17标准</li>"
        "</ul>"
    );

    QMessageBox::information(this, "使用帮助", helpText);
}

void SonyResponsiveDemo::runSystemTest()
{
    if (!m_testRunner) {
        return;
    }

    statusBar()->showMessage("正在运行系统测试...");

    // 在后台线程中运行测试
    QTimer::singleShot(100, this, [this]() {
        m_testRunner->runAllTests();

        QString message;
        if (m_testRunner->allTestsPassed()) {
            message = "🎉 系统测试全部通过！";
            statusBar()->showMessage(message, 5000);
        } else {
            message = QString("⚠️ 部分测试失败 (%1/%2)")
                       .arg(m_testRunner->getFailedTests().size())
                       .arg(m_testRunner->allTestsPassed() ? 0 : 1);
            statusBar()->showMessage(message, 5000);
        }

        // 显示详细结果
        QString summary = m_testRunner->getTestSummary();
        QStringList failedTests = m_testRunner->getFailedTests();

        QString report = QString(
            "<h3>🧪 系统测试结果</h3>"
            "<p><b>%1</b></p>"
        ).arg(summary);

        if (!failedTests.isEmpty()) {
            report += "<p><b>失败的测试:</b></p><ul>";
            for (const QString& test : failedTests) {
                report += QString("<li>%1</li>").arg(test);
            }
            report += "</ul>";
        }

        QMessageBox::information(this, "测试结果", report);
    });
}

void SonyResponsiveDemo::showPerformanceMonitor()
{
    QMessageBox::information(this, "性能监控器",
        "📊 性能监控器已在主界面中运行\n\n"
        "实时监控指标:\n"
        "• FPS (帧率)\n"
        "• 内存使用量\n"
        "• 缓存命中率\n"
        "• 动画性能\n"
        "• 自动优化建议\n\n"
        "所有性能警告和优化建议都会显示在性能日志中。");
}

void SonyResponsiveDemo::resetToDefaults()
{
    // 重置所有设置到默认值
    if (m_exampleWidget) {
        // 这里应该重置所有控件到默认状态
        statusBar()->showMessage("已重置到默认设置", 3000);
    }
}

int main(int argc, char *argv[])
{
    QApplication app(argc, argv);

    // 设置应用程序信息
    app.setApplicationName("SonyResponsiveDemo");
    app.setApplicationVersion("1.0");
    app.setOrganizationName("Sony Ive Design Lab");


    try {
        SonyResponsiveDemo demo;
        demo.show();


        int result = app.exec();

        return result;

    } catch (const std::exception& e) {
        qCritical() << "程序启动失败:" << e.what();
        QMessageBox::critical(nullptr, "错误",
            QString("程序启动失败:\n%1").arg(e.what()));
        return -1;
    }
}

#include "sonyresponsive_demo.moc"